<?php
global $_W, $_GPC;
$weid = $this->_weid;
$from_user = $this->_fromuser;
$setting = $this->getSetting();
$marketingid = intval($_GPC['marketingid']);

$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($setting['auth_mode'] == 1 || empty($setting)) {
    $method = 'addstore'; //method
    $host = $this->getOAuthHost();
    $authurl = $host . 'app/' . $this->createMobileUrl($method, array('storeid' => $storeid, 'marketingid' => $marketingid, 'op' => $op),
            true) .
        '&authkey=1';
    $url = $host . 'app/' . $this->createMobileUrl($method, array('storeid' => $storeid, 'marketingid' => $marketingid, 'op' => $op), true);
    if (isset($_COOKIE[$this->_auth2_openid])) {
        $from_user = $_COOKIE[$this->_auth2_openid];
        $nickname = $_COOKIE[$this->_auth2_nickname];
        $headimgurl = $_COOKIE[$this->_auth2_headimgurl];
    } else {
        if (isset($_GPC['code'])) {
            $userinfo = $this->oauth2($authurl);
            if (!empty($userinfo)) {
                $from_user = $userinfo["openid"];
                $nickname = $userinfo["nickname"];
                $headimgurl = $userinfo["headimgurl"];
            } else {
                message('授权失败!');
            }
        } else {
            if (!empty($this->_appsecret)) {
                $this->getCode($url);
            }
        }
    }
} else {
    load()->model('mc');
    if (empty($_W['fans']['nickname'])) {
        mc_oauth_userinfo();
    }
    $from_user = $_W['fans']['openid'];
    $nickname = $_W['fans']['nickname'];
    $headimgurl = $_W['fans']['tag']['avatar'];
}

$fans = $this->getFansByOpenid($from_user);
if (empty($fans)) {
    $this->addFans($nickname, $headimgurl);
} else {
    $this->updateFans($nickname, $headimgurl, $fans['id']);
}
$fans = $this->getFansByOpenid($from_user);
if ($fans['status'] == 0) {
    die('系统调试中！');
}

if (empty($from_user)) {
    message('会话已过期，请重新发送关键字!');
}

$area = pdo_fetchall("SELECT * FROM " . tablename($this->table_area) . " where weid = :weid {$where_type} ORDER BY displayorder DESC", array(':weid' => $weid));
$shoptype = pdo_fetchall("SELECT * FROM " . tablename($this->table_type) . " where weid = :weid {$where_type} ORDER BY displayorder DESC", array(':weid' => $weid));

//市场专员
$marketing = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketing') . " WHERE weid = :weid AND id=:id ORDER BY id DESC", array(':weid' => $this->_weid, ':id' => $marketingid));

$is_check = 0;
if (!empty($marketing)) { //有市场专员
    if ($marketing['from_user'] != $from_user) {
        $is_check = 1;//是店主需要检查
    }
}
if ($is_check == 1) {
    $checkuser = pdo_fetch("SELECT * FROM " . tablename($this->table_stores) . " WHERE weid = :weid AND from_user=:from_user LIMIT 1;", array(':weid' => $this->_weid, ':from_user' => $from_user));
    if ($checkuser) {
        message('您已经申请过入驻了，不需要重新申请！' . $checkuser['id'] );
    }
}

if (checksubmit('submit')) {
    $data = array(
        'weid' => $weid,
        'schoolid' => 0,
        'marketingid' => $marketingid,
        'areaid' => intval($_GPC['area']),
        'is_hasten' => intval($_GPC['is_hasten']),
        'typeid' => intval($_GPC['type']),
        'title' => trim($_GPC['title']),
        'info' => trim($_GPC['info']),
        'deliverytime' => intval($_GPC['deliverytime']),
        'from_user' => $from_user,
        'content' => trim($_GPC['content']),
        'tel' => trim($_GPC['tel']),
        'announce' => trim($_GPC['announce']),
        'reservation_announce' => trim($_GPC['reservation_announce']),
        'address' => trim($_GPC['address']),
        'lng' => trim($_GPC['lng']),
        'lat' => trim($_GPC['lat']),
        'is_show' => -1,
        'is_list' => intval($_GPC['is_list']),
        'is_wxapp_check' => 0,
        'place' => trim($_GPC['place']),
        'qq' => trim($_GPC['qq']),
        'weixin' => trim($_GPC['weixin']),
        'hours' => trim($_GPC['hours']),
        'consume' => trim($_GPC['consume']),
        'level' => intval($_GPC['level']),
        'is_delivery_time' => intval($_GPC['is_delivery_time']),
        'is_speaker' => intval($_GPC['is_speaker']),
        'enable_wifi' => intval($_GPC['enable_wifi']),
        'enable_card' => intval($_GPC['enable_card']),
        'enable_room' => intval($_GPC['enable_room']),
        'enable_park' => intval($_GPC['enable_park']),
        'is_meal' => intval($_GPC['is_meal']),
        'is_delivery' => intval($_GPC['is_delivery']),
        'is_delivery_mode1' => $is_delivery_mode1,
        'is_delivery_mode2' => $is_delivery_mode2,
        'is_more_meal' => intval($_GPC['is_more_meal']),
        'is_snack' => intval($_GPC['is_snack']),
        'is_queue' => intval($_GPC['is_queue']),
        'is_add_dish' => intval($_GPC['is_add_dish']),
        'default_jump' => intval($_GPC['default_jump']),
        'default_jump_url' => trim($_GPC['default_jump_url']),
        'is_intelligent' => intval($_GPC['is_intelligent']),
        'is_reservation' => intval($_GPC['is_reservation']),
        'is_sms' => intval($_GPC['is_sms']),
        'is_hot' => intval($_GPC['is_hot']),
        'is_check_user' => intval($_GPC['is_check_user']),
        'is_dispatcharea' => intval($_GPC['is_dispatcharea']),
        'is_bank_pay' => intval($_GPC['is_bank_pay']),
        'bank_pay_id' => intval($_GPC['bank_pay_id']),
        'is_vtiny_bankpay' => intval($_GPC['is_vtiny_bankpay']),
        'jxkj_pay_id' => intval($_GPC['jxkj_pay_id']),
        'is_jxkj_unipay' => intval($_GPC['is_jxkj_unipay']),
        'jxkj_pay_name' => trim($_GPC['jxkj_pay_name']),
        'vtiny_bankpay_id' => intval($_GPC['vtiny_bankpay_id']),
        'vtiny_bankpay_url' => trim($_GPC['vtiny_bankpay_url']),
        'is_ld_wxserver' => intval($_GPC['is_ld_wxserver']),
        'ld_wxserver_url' => trim($_GPC['ld_wxserver_url']),
        'business_id' => intval($_GPC['business_id']),
        'is_business' => intval($_GPC['is_business']),
        'is_savewine' => intval($_GPC['is_savewine']),
        'is_operator1' => intval($_GPC['is_operator1']),
        'is_operator2' => intval($_GPC['is_operator2']),
        'is_fengniao_area' => intval($_GPC['is_fengniao_area']),
        'is_meal_pay_confirm' => intval($_GPC['is_meal_pay_confirm']),
        'notice_space_time' => intval($_GPC['notice_space_time']),
        'btn_reservation' => trim($_GPC['btn_reservation']),
        'btn_eat' => trim($_GPC['btn_eat']),
        'btn_delivery' => trim($_GPC['btn_delivery']),
        'btn_snack' => trim($_GPC['btn_snack']),
        'btn_queue' => trim($_GPC['btn_queue']),
        'btn_intelligent' => trim($_GPC['btn_intelligent']),
        'btn_shouyin' => trim($_GPC['btn_shouyin']),
        'coupon_title1' => trim($_GPC['coupon_title1']),
        'coupon_title2' => trim($_GPC['coupon_title2']),
        'coupon_title3' => trim($_GPC['coupon_title3']),
        'coupon_link1' => trim($_GPC['coupon_link1']),
        'coupon_link2' => trim($_GPC['coupon_link2']),
        'coupon_link3' => trim($_GPC['coupon_link3']),
        'remarkinfo' => trim($_GPC['remarkinfo']),
        'listinfo' => trim($_GPC['listinfo']),
        'is_locktables' => intval($_GPC['is_locktables']),
        'is_brand' => intval($_GPC['is_brand']),
        'brandname' => trim($_GPC['brandname']),
        'wechat' => intval($_GPC['wechat']),
        'alipay' => intval($_GPC['alipay']),
        'credit' => intval($_GPC['credit']),
        'delivery' => intval($_GPC['delivery']),
        'is_auto_confirm' => intval($_GPC['is_auto_confirm']),
        'is_order_autoconfirm' => intval($_GPC['is_order_autoconfirm']),
        'delivery_isnot_today' => intval($_GPC['delivery_isnot_today']),
        'sendingprice' => trim($_GPC['sendingprice']),
        'dispatchprice' => trim($_GPC['dispatchprice']),
        'kefu_qrcode' => trim($_GPC['kefu_qrcode']),
        'freeprice' => trim($_GPC['freeprice']),
        'begintime' => '08:00',
        'endtime' => '18:00',
        'begintime1' => trim($_GPC['begintime1']),
        'endtime1' => trim($_GPC['endtime1']),
        'begintime2' => trim($_GPC['begintime2']),
        'endtime2' => trim($_GPC['endtime2']),
        'updatetime' => TIMESTAMP,
        'is_add_order' => intval($_GPC['is_add_order']),
        'is_delivery_nowtime' => intval($_GPC['is_delivery_nowtime']),
        'is_order_tip' => intval($_GPC['is_order_tip']),
        'dateline' => TIMESTAMP,
        'delivery_within_days' => intval($_GPC['delivery_within_days']),
        'delivery_radius' => floatval($_GPC['delivery_radius']),
        'not_in_delivery_radius' => intval($_GPC['not_in_delivery_radius']),
        'is_tea_money' => intval($_GPC['is_tea_money']),
        'is_floor_money' => intval($_GPC['is_floor_money']),
        'is_shouyin' => intval($_GPC['is_shouyin']),
        'tea_money' => floatval($_GPC['tea_money']),
        'tea_tip' => trim($_GPC['tea_tip']),
        'floor_money' => floatval($_GPC['floor_money']),
        'floor_tip' => trim($_GPC['floor_tip']),
        'default_user_count' => intval($_GPC['default_user_count']),
        'is_jueqi_ymf' => intval($_GPC['is_jueqi_ymf']),
        'is_newlimitprice' => intval($_GPC['is_newlimitprice']),
        'is_oldlimitprice' => intval($_GPC['is_oldlimitprice']),
        'is_delivery_distance' => intval($_GPC['is_delivery_distance']),
        'is_sys_delivery' => intval($_GPC['is_sys_delivery']),
        'username' => trim($_GPC['username']),
        'password' => trim($_GPC['password']),
    );

    if (!empty($_FILES['thumb']['name'])) {
        $data['logo'] = $this->uploadImg('thumb');
    }

    if (empty($data['title'])) {
        message('请输入门店名称！');
    }
    if (empty($data['tel'])) {
        message('请输入联系电话！');
    }
    if (empty($data['address'])) {
        message('请输入联系地址！');
    }
    if (!preg_match(REGULAR_USERNAME, $data['username'])) {
        message('必须输入用户名，格式为 3-15 位字符，可以包括汉字、字母（不区分大小写）、数字、下划线和句点。');
    }
    load()->model('user');
    if (user_check(array('username' => $data['username']))) {
        message('非常抱歉，此用户名已经被注册，你需要更换注册名称！');
    }
    if (!empty($_GPC['password'])) {
        $data['password'] = $_GPC['password'];
        if (istrlen($data['password']) < 8) {
            message('必须输入密码，且密码长度不得低于8位。');
        }
    } else {
        message('请输入登陆密码！');
    }

    if ($data['marketingid'] > 0) {
        $data['is_show'] = 1;
    }

    if (empty($id)) {
        pdo_insert($this->table_stores, $data);
        $id = pdo_insertid();
    } else {
        unset($data['dateline']);
        pdo_update($this->table_stores, $data, array('id' => $id));
    }
    if ($data['marketingid'] > 0) { //市场专员
        //专员门店数量
        $total = pdo_fetchcolumn("SELECT COUNT(1) FROM " . tablename($this->table_stores) . " WHERE
            weid=:weid AND marketingid=:marketingid AND is_show=1 ", array(':marketingid' => $data['marketingid'], ':weid' => $weid));
        //操作等级
        $level = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketinglevel') . " WHERE weid={$weid} AND {$total}<=storecount ORDER BY
storecount ASC LIMIT 1;");
        pdo_update('weisrc_dish_marketing',
            array('levelid' => $level['id'],'storetotal' => $total),
            array('id' => $data['marketingid'])
        );

        //审核通过添加店长管理员
        $user = array();
        $user['username'] = trim($data['username']);
        $user['password'] = $data['password'];
        $user['remark'] = '';
        $user['status'] = 2;
        $user['groupid'] = -1;
        if (empty($users)) {
            if (user_check(array('username' => $user['username']))) {
                message('非常抱歉，此用户名已经被注册，你需要更换注册名称！');
            }

            if (IMS_VERSION >= '2.0.0') {
                $uid = user_register($user, 'admin');
            } else {
                $uid = user_register($user);
            }

            if ($uid > 0) {
                unset($user['password']);
                //operator
                $data = array(
                    'uniacid' => $this->_weid,
                    'uid' => $uid,
                    'role' => 'operator',
                );
                $exists = pdo_fetch("SELECT * FROM " . tablename('uni_account_users') . " WHERE uid = :uid AND uniacid = :uniacid", array(':uniacid' => $this->_weid, ':uid' => $uid));
                if (empty($exists)) {
                    pdo_insert('uni_account_users', $data);
                }

                $data = array(
                    'uid' => $uid,
                    'weid' => $this->_weid,
                    'storeid' => $id,
                    'from_user' => $from_user,
                    'email' => '',
                    'mobile' => '',
                    'pay_account' => trim($_GPC['pay_account']),
                    'status' => 2,
                    'remark' => '',
                    'dateline' => TIMESTAMP,
                    'username' => '',
                    'role' => 1,
                    'is_admin_order' => 1,
                    'is_notice_order' => 1,
                    'is_notice_service' => 1,
                    'is_notice_boss' => 1,
                    'is_notice_queue' => 1,
                    'is_admin_goods' => 1,
                    'is_admin_money' => 1,
                    'is_admin_goodscate' => 1,
                    'is_admin_storeopen' => 1,
                );

                if ($data['role'] == 1) {
                    //permission
                    pdo_insert('users_permission', array(
                        'uid' => $uid,
                        'uniacid' => $this->_weid,
                        'url' => '',
                        'type' => 'weisrc_dish',
                        'permission' => 'weisrc_dish_menu_stores2'
                    ));
                } else {
                    //permission
                    pdo_insert('users_permission', array(
                        'uid' => $uid,
                        'uniacid' => $this->_weid,
                        'url' => '',
                        'type' => 'weisrc_dish',
                        'permission' => 'weisrc_dish_menu_stores2|weisrc_dish_menu_account|weisrc_dish_menu_type|weisrc_dish_menu_ad|weisrc_dish_menu_area|weisrc_dish_menu_notice'
                    ));
                }
                pdo_insert($this->table_account, $data);
                $url = $this->createMobileUrl('adminstore', array(), true);
                message('入驻成功!', $url, 'success');
            } else {
                message('注册失败，请稍候重试或联系网站管理员解决！');
            }
        }
        message('审核用户失败，请稍候重试或联系网站管理员解决！');
    } else {
        $url = $this->createMobileUrl('usercenter', array('op' => 'display'), true);
        message('已经提交申请，请等待审核！', $url, 'success');
    }
}
include $this->template($this->cur_tpl . '/addstore');


